//
//  YMLanguageUtil.m
//  YMMedical
//
//  Created by y s on 2024/1/20.
//

#import "YMLanguageUtil.h"

@interface YMLanguageUtil ()

@end


@implementation YMLanguageUtil

+ (instancetype)shared{
   static dispatch_once_t onceToken;
   static YMLanguageUtil *sharedInstance;
   dispatch_once(&onceToken, ^{
       sharedInstance = [[YMLanguageUtil alloc] init];
   });
   return sharedInstance;
}

- (instancetype)init{
   if (self = [super init]){
       
   }
   return self;
}

- (void)launch{
    //0: 跟随系统 ,1: 中文 , 2:  English
    NSInteger type = [SLocalUtil intForKey:kSetting_Language valid:0];
    [YMLanguageUtil setLanguagueWithType:type];
}
+ (NSString *)getLanguageWithKey:(NSString *)key{
    NSString *language = [SLocalUtil stringForKey:App_Language];
    if (language != nil && language.length > 0){
        NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
        return [[NSBundle bundleWithPath:path] localizedStringForKey:(key) value:nil table:@"Language"];
    }
    return @"";
}

/** 获取设置的语言名称 0: 跟随系统 ,1: 中文 , 2:  English*/
+ (NSString *)getSettingLanguage{
    NSInteger type = [SLocalUtil intForKey:kSetting_Language valid:0];
    if (type == 1) {
        return KLanguage(@"language_chinese");
    }else if (type == 2) {
        return KLanguage(@"language_english");
    }else{
        return KLanguage(@"language_system");
    }
}
/** 根据设置类型设置语言 */
+ (void)setLanguagueWithType:(NSInteger)type {
    [SLocalUtil setInt:type forKey:kSetting_Language];
    if (type == 1) {
        [SLocalUtil setString:@"zh-Hans" forKey:App_Language];
    }else if (type == 2) {
        [SLocalUtil setString:@"en" forKey:App_Language];
    }else{
        //获得当前语言
        NSArray *languages = [NSLocale preferredLanguages];
        NSString *language = [languages objectAtIndex:0];
        if([language hasPrefix:@"en"]){
            [SLocalUtil setString:@"en" forKey:App_Language];
        }else{
            [SLocalUtil setString:@"zh-Hans" forKey:App_Language];
        }
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (BOOL)isEnglish{
    NSString *language = [SLocalUtil stringForKey:App_Language];
    if ([language hasPrefix:@"en"]) {
        return YES;
    }else{
        return NO;
    }
}




@end
